package com.zjyd.douyangcommonredis.config;


import io.lettuce.core.api.StatefulConnection;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis相关配置
 */
@Configuration
public class RedisConfig {

    // 硬编码Redis连接参数
    private final String redisHost = "115.190.141.247";
    private final int redisPort = 6379;
    private final String redisPassword = "Lsx2023@XGD";
    // 数据库 可以选0-15
    private final int redisDatabase = 0;
    private final Duration redisTimeout = Duration.ofSeconds(30); // 30s超时

    // 硬编码Lettuce连接池参数
    private final int poolMaxActive = 200;
    private final Duration poolMaxWait = Duration.ofMillis(-1); // -1ms（无限制）
    private final int poolMaxIdle = 10;
    private final int poolMinIdle = 0;

    /**
     * 创建Redis连接工厂（Lettuce实现）
     */
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        // 1. 配置Redis服务器信息
        RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
        standaloneConfig.setHostName(redisHost);
        standaloneConfig.setPort(redisPort);
        standaloneConfig.setPassword(RedisPassword.of(redisPassword));
        standaloneConfig.setDatabase(redisDatabase);

        // 2. 配置连接池（泛型指定为父类 StatefulConnection<?, ?>，解决类型匹配）
        GenericObjectPoolConfig<StatefulConnection<?, ?>> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setMaxTotal(poolMaxActive);
        poolConfig.setMaxWait(poolMaxWait);
        poolConfig.setMaxIdle(poolMaxIdle);
        poolConfig.setMinIdle(poolMinIdle);

        // 3. 配置Lettuce客户端（此时poolConfig类型与期望完全匹配）
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .poolConfig(poolConfig)
                .commandTimeout(redisTimeout)
                .build();

        // 4. 创建连接工厂
        return new LettuceConnectionFactory(standaloneConfig, clientConfig);
    }

    /**
     * 创建RedisTemplate（默认序列化配置）
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        // 配置序列化器（key用String，value用JSON）
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();

        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(jsonSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(jsonSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

